<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link href="static/assets/global/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <link href="static/assets/global/css/components.css" rel="stylesheet" type="text/css">
    <link href="static/assets/admin/pages/css/login.css" rel="stylesheet" type="text/css">
    <script src="static/assets/global/plugins/jquery-1.11.0.min.js" type="text/javascript"></script>
</head>
<body css="login" style="width: 400px; ">
<div class="content" >
    <div id="promoStartDateContainer" class="form-group">
        <label style="color: red;" class="control-lable">秒杀开始时间</label>
        <div>
            <label style="color: red;" class ="control-lable" type="text"  id="promoStartDate">
        </div>
    </div>
    <div class="form-group">
        <label class="control-lable">商品名</label>
        <div>
            <label class ="control-lable" type="text"  name="title" id="title">
        </div>
    </div>
    <div class="form-group">
        <label class="control-lable">商品描述</label>
        <div>
            <label class ="control-lable" type="text"  id="description">
        </div>
    </div>
    <div class="form-group">
        <label class="control-lable">价格</label>
        <div>
            <label class ="control-lable" type="text"  id="price">
        </div>
    </div>
    <div id="promoPriceContainer" class="form-group">
        <label style="color: red;" class="control-lable">秒杀价格</label>
        <div>
            <label style="color: red;" class ="control-lable" type="text"  id="promoPrice">
        </div>
    </div>
    <div class="form-group">
        <div>
            <img style="width:200px;height:auto;" id="imgUrl">
        </div>
    </div>
    <div class="form-group">
        <label class="control-lable">库存</label>
        <div>
            <label class ="control-lable" type="text"  id="stock">
        </div>
    </div>
    <div class="form-group">
        <label class="control-lable">销量</label>
        <div>
            <label class ="control-lable" type="text"  id="sales">
        </div>
    </div>
    <div class="form-actions">
        <button class="btn blue" id="createorder" type="submit">
            下单
        </button>
    </div>
</div>
</body>
<script>

function getParam(paramName) {
    paramValue = "", isFound = !1;
    if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
        arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&"), i = 0;
        while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++
    }
    return paramValue == "" && (paramValue = null), paramValue
}
var g_itemVO={};

    jQuery(document).ready(function (){
        $("#createorder").on("click",function(){
            $.ajax({
             type:"POST",
             contentType:"application/x-www-form-urlencoded",
             url:"http://localhost:8080/order/createorder",
             data:{
                 "itemId":g_itemVO.id,
                 "amount":1,
                 "promoId":g_itemVO.promoId
             },
             xhrFields:{withCredentials:true},
             success:function (data){
                 if(data.status=="success"){
                     alert("下单成功");
                     window.location.reload();

                 }else{
                     alert("下单失败，原因为"+data.data.errMsg);
                     if(data.data.errCode==20003){
                         window.location.href="login.html";
                     }
                 }
             },
             error:function (data) {
                 alert("下单失败，原因为"+data.responseText);
             }
         }); 
        });
         $.ajax({
             type:"GET",
             url:"http://localhost:8080/item/get",
             data:{
                 "id":getParam("id"),
             },
             xhrFields:{withCredentials:true},
             success:function (data){
                 if(data.status=="success"){
                     g_itemVO = data.data;
                     reloadDom();
                     setInterval(reloadDom,1000);
                 }else{
                     alert("获取信息失败，原因为"+data.data.errMsg)
                 }
             },
             error:function (data) {
                 alert("获取信息失败，原因为"+data.responseText);
             }
         });
    });

    function reloadDom(){
        
        $("#title").text(g_itemVO.title);
        $("#description").text(g_itemVO.description);
        $("#stock").text(g_itemVO.stock);
        $("#price").text(g_itemVO.price);
        $("#imgUrl").attr("src",g_itemVO.imgUrl);
        $("#sales").text(g_itemVO.sales);
        if(g_itemVO.promoStatus == 1){

            var startTime = g_itemVO.startDate.replace(new RegExp("-","gm"),"/");
            startTime = (new Date(startTime)).getTime();
            var nowTime = Date.parse(new Date());
            var delta = (startTime-nowTime)/1000;

            if(delta<=0){
                g_itemVO.promoStatus = 2;
                reloadDom();
            }

            $("#promoStartDate").text(g_itemVO.startDate+"   倒计时："+delta+"秒");
            $("#promoPrice").text(g_itemVO.promoPrice);

            $("#createorder").attr("disabled",true);
        }else if(g_itemVO.promoStatus==2){
            $("#promoStartDate").text("秒杀正在进行");
            $("#promoPrice").text(g_itemVO.promoPrice);

            $("#createorder").attr("disabled",false);
        }
    }
</script>
</html>